package com.nayuta.aipo.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * JDBC Connect Utility.
 */
public class JDBCUtils {

	public static ConnectionBean querySql(String query) {
		ConnectionBean bean = new ConnectionBean();

		try {
			Class.forName("org.postgresql.Driver");

			bean.setConn(DriverManager.getConnection(
					"jdbc:postgresql://localhost/org001", "aipo_postgres", "b58k5l8XOUrN"));
			bean.setStmt(bean.getConn().createStatement());

			bean.setRs(bean.getStmt().executeQuery(query));
		} catch (ClassNotFoundException e) {
		} catch (SQLException e) {
			
		} finally {
			
		}
		
		return bean;
	}

	public static void closeConnect(ConnectionBean bean) {
		try {
			if (bean.rs != null) {
				bean.rs.close();
			}
			if (bean.stmt != null) {
				bean.stmt.close();
			}
			if (bean.conn != null) {
				bean.conn.close();
			}
		} catch (SQLException e) {
			
		}
	}
	
	public static class ConnectionBean {
		private Connection conn = null;
		private Statement stmt = null;
		private ResultSet rs = null;
		public Connection getConn() {
			return conn;
		}
		public void setConn(Connection conn) {
			this.conn = conn;
		}
		public Statement getStmt() {
			return stmt;
		}
		public void setStmt(Statement stmt) {
			this.stmt = stmt;
		}
		public ResultSet getRs() {
			return rs;
		}
		public void setRs(ResultSet rs) {
			this.rs = rs;
		}
	}
}
